//
// Created by martin on 2/6/22.
//

#ifndef MUDUO_COUNTDOWNLATCH_H
#define MUDUO_COUNTDOWNLATCH_H

#include "muduo/base/noncopyable.h"
#include "muduo/base/Mutex.h"
#include "muduo/base/Condition.h"

namespace muduo
{

/**
 * Count down from a count user_ specified to zero.
 *
 * Thread safe.
 */
class CountDownLatch
{
public:
    explicit CountDownLatch(int count);

    /**
     * Wait until count_ decrease to 0
     */
    void wait();
    /**
     * Decrease 1 in count_
     */
    void countDown();

    /**
     * Return the value of count_
     */
    int getCount() const;

private:
    mutable MutexLock mutex_;
    Condition cond_ GUARDED_BY(mutex_);
    int count_ GUARDED_BY(mutex_);
};

} // namespace muduo

#endif //MUDUO_COUNTDOWNLATCH_H
